package com.nlp.visualization.service.impl.backend;

import com.nlp.visualization.dao.backend.IApiCountDao;
import com.nlp.visualization.pojo.db.TApiStatistics;
import com.nlp.visualization.service.backEnd.IApiCountService;
import com.nlp.visualization.service.base.ServiceSupport;
import com.nlp.visualization.service.base.ServiceSupportImpl;
import com.nlp.visualization.utils.TimeHelper;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LXM
 * @Title: NLPVisualizationTools
 * @Description:
 * @date 2018/5/16下午3:22
 */
@Service("apiCountService")
public class ApiCountServiceImpl extends ServiceSupportImpl<TApiStatistics> implements IApiCountService {

    @Autowired
    private IApiCountDao apiDao;

    private static final String SEG = "词法分析接口";

    private static final String SEN = "句法分析接口";

    private static final String DISCOURSE = "语篇分析接口";

    private static final String WEIBO = "微博分析接口";

    /**
     * 统计当前api的使用次数
     */
    @Override
    public List countApiByToday() {


        List resList = new ArrayList();


        Long start = TimeHelper.getTimesmorning().getTime();
        Long end = 0L;
        for (int i = 0; i < 12; i++) {
            List tmpList = new ArrayList();
            long lastend = end;
            end = start + i * 60 * 60 * 2 * 1000;
            List todayList;
            if (lastend == 0)
                todayList = apiDao.countByTime(TimeHelper.getTimesmorning().getTime(), end);
            else
                todayList = apiDao.countByTime(lastend, end);
            Map map = new HashedMap();
            List list = new ArrayList();
            map.put("time", i * 2 + ":00");

            //统计少了哪个接口的数据是没有的
            for (Object ob : todayList) {
                Object[] ob1 = (Object[]) ob;
                tmpList.add(ob1[0]);
            }

            if (!tmpList.contains(SEG)) {
                Map map1 = new HashedMap();
                map1.put("name", SEG);
                map1.put("count", 0);

                list.add(map1);
            }
            if (!tmpList.contains(SEN)) {
                Map map1 = new HashedMap();
                map1.put("name", SEN);
                map1.put("count", 0);
                list.add(map1);
            }
            if (!tmpList.contains(DISCOURSE)) {
                Map map1 = new HashedMap();
                map1.put("name", DISCOURSE);
                map1.put("count", 0);
                list.add(map1);
            }
            if (!tmpList.contains(WEIBO)) {
                Map map1 = new HashedMap();
                map1.put("name", WEIBO);
                map1.put("count", 0);
                list.add(map1);
            }

            for (Object ob : todayList) {
                Object[] ob1 = (Object[]) ob;
                Map map1 = new HashedMap();
                map1.put("name", ob1[0]);
                map1.put("count", ob1[1]);
                list.add(map1);
            }
            map.put("count", list);
            resList.add(map);
        }

        return resList;
    }

    /**
     * 统计本周api的使用次数
     */
    @Override
    public List countApiByWeek() {
        List resList = new ArrayList();

        Long start = TimeHelper.getTimesWeekmorning().getTime()+ 60 * 60 * 24 * 1000;
        Long end = 0L;
        for (int i = 0; i < 7; i++) {
            List tmpList = new ArrayList();
            long lastend = end;
            end = start + i * 60 * 60 * 24 * 1000;
            List todayList = apiDao.countByTime(lastend, end);
            Map map = new HashedMap();
            List list = new ArrayList();
            map.put("day", i);

            //统计少了哪个接口的数据是没有的
            for (Object ob : todayList) {
                Object[] ob1 = (Object[]) ob;
                tmpList.add(ob1[0]);
            }

            if (!tmpList.contains(SEG)) {
                Map map1 = new HashedMap();
                map1.put("name", SEG);
                map1.put("count", 0);

                list.add(map1);
            }
            if (!tmpList.contains(SEN)) {
                Map map1 = new HashedMap();
                map1.put("name", SEN);
                map1.put("count", 0);
                list.add(map1);
            }
            if (!tmpList.contains(DISCOURSE)) {
                Map map1 = new HashedMap();
                map1.put("name", DISCOURSE);
                map1.put("count", 0);
                list.add(map1);
            }
            if (!tmpList.contains(WEIBO)) {
                Map map1 = new HashedMap();
                map1.put("name", WEIBO);
                map1.put("count", 0);
                list.add(map1);
            }

            for (Object ob : todayList) {
                Object[] ob1 = (Object[]) ob;
                Map map1 = new HashedMap();
                map1.put("name", ob1[0]);
                map1.put("count", ob1[1]);
                list.add(map1);
            }
            map.put("count", list);
            resList.add(map);
        }

        return resList;
    }

    /**
     * 统计本月api的使用次数
     */
    @Override
    public List countApiByMonth() {
        List resList = new ArrayList();

        Long start = TimeHelper.getTimesMonthmorning().getTime()+ 60 * 60 * 24 * 1000;
        int days = -(int) (TimeHelper.getTimesMonthnight().getTime() - TimeHelper.getTimesMonthmorning().getTime()) / (60 * 60 * 24 * 1000);
        Long end = 0L;
        for (int i = 0; i < days; i++) {
            List tmpList = new ArrayList();
            long lastend = end;
            end = start + i * 60 * 60 * 24 * 1000;
            List todayList = apiDao.countByTime(lastend, end);
            Map map = new HashedMap();
            List list = new ArrayList();
            map.put("day", i);

            //统计少了哪个接口的数据是没有的
            for (Object ob : todayList) {
                Object[] ob1 = (Object[]) ob;
                tmpList.add(ob1[0]);
            }

            if (!tmpList.contains(SEG)) {
                Map map1 = new HashedMap();
                map1.put("name", SEG);
                map1.put("count", 0);

                list.add(map1);
            }
            if (!tmpList.contains(SEN)) {
                Map map1 = new HashedMap();
                map1.put("name", SEN);
                map1.put("count", 0);
                list.add(map1);
            }
            if (!tmpList.contains(DISCOURSE)) {
                Map map1 = new HashedMap();
                map1.put("name", DISCOURSE);
                map1.put("count", 0);
                list.add(map1);
            }
            if (!tmpList.contains(WEIBO)) {
                Map map1 = new HashedMap();
                map1.put("name", WEIBO);
                map1.put("count", 0);
                list.add(map1);
            }

            for (Object ob : todayList) {
                Object[] ob1 = (Object[]) ob;
                Map map1 = new HashedMap();
                map1.put("name", ob1[0]);
                map1.put("count", ob1[1]);
                list.add(map1);
            }
            map.put("count", list);
            resList.add(map);
        }

        return resList;
    }

    /**
     * 统计全部的api的使用次数图表
     *
     * @return
     */
    @Override
    public List countApi() {
        return null;
    }

    /**
     * 获取当前服务器的信息
     *
     * @return
     */
    @Override
    public List systemStates() {
        return null;
    }

    /**
     * 计算今天的API流量占比
     *
     * @return
     */
    @Override
    public List countEachApiTotalByToday() {
        List resList = new ArrayList();


        Long start = TimeHelper.getTimesmorning().getTime();
        Long end = TimeHelper.getTimesnight().getTime();
        List list = new ArrayList();
        List tmpList = new ArrayList();
        List todayList = apiDao.countByTime(start, end);
        Map map = new HashedMap();

        //统计少了哪个接口的数据是没有的
        for (Object ob : todayList) {
            Object[] ob1 = (Object[]) ob;
            tmpList.add(ob1[0]);
        }

        if (!tmpList.contains(SEG)) {
            Map map1 = new HashedMap();
            map1.put("name", SEG);
            map1.put("count", 0);

            list.add(map1);
        }
        if (!tmpList.contains(SEN)) {
            Map map1 = new HashedMap();
            map1.put("name", SEN);
            map1.put("count", 0);
            list.add(map1);
        }
        if (!tmpList.contains(DISCOURSE)) {
            Map map1 = new HashedMap();
            map1.put("name", DISCOURSE);
            map1.put("count", 0);
            list.add(map1);
        }
        if (!tmpList.contains(WEIBO)) {
            Map map1 = new HashedMap();
            map1.put("name", WEIBO);
            map1.put("count", 0);
            list.add(map1);
        }

        for (Object ob : todayList) {
            Object[] ob1 = (Object[]) ob;
            Map map1 = new HashedMap();
            map1.put("name", ob1[0]);
            map1.put("count", ob1[1]);
            list.add(map1);
        }
        map.put("count", list);
        resList.add(map);

        return resList;
    }

    /**
     * 计算本周的API流量占比
     *
     * @return
     */
    @Override
    public List countEachApiTotalByWeek() {
        List resList = new ArrayList();


        Long start = TimeHelper.getTimesWeekmorning().getTime();
        Long end = TimeHelper.getTimesWeeknight().getTime();
        List list = new ArrayList();
        List tmpList = new ArrayList();
        List todayList = apiDao.countByTime(start, end);
        Map map = new HashedMap();

        //统计少了哪个接口的数据是没有的
        for (Object ob : todayList) {
            Object[] ob1 = (Object[]) ob;
            tmpList.add(ob1[0]);
        }

        if (!tmpList.contains(SEG)) {
            Map map1 = new HashedMap();
            map1.put("name", SEG);
            map1.put("count", 0);

            list.add(map1);
        }
        if (!tmpList.contains(SEN)) {
            Map map1 = new HashedMap();
            map1.put("name", SEN);
            map1.put("count", 0);
            list.add(map1);
        }
        if (!tmpList.contains(DISCOURSE)) {
            Map map1 = new HashedMap();
            map1.put("name", DISCOURSE);
            map1.put("count", 0);
            list.add(map1);
        }
        if (!tmpList.contains(WEIBO)) {
            Map map1 = new HashedMap();
            map1.put("name", WEIBO);
            map1.put("count", 0);
            list.add(map1);
        }

        for (Object ob : todayList) {
            Object[] ob1 = (Object[]) ob;
            Map map1 = new HashedMap();
            map1.put("name", ob1[0]);
            map1.put("count", ob1[1]);
            list.add(map1);
        }
        map.put("count", list);
        resList.add(map);

        return resList;
    }

    /**
     * 计算本月的API流量占比
     *
     * @return
     */
    @Override
    public List countEachApiTotalByMonth() {
        List resList = new ArrayList();


        Long start = TimeHelper.getTimesMonthmorning().getTime();
        Long end = TimeHelper.getTimesMonthnight().getTime();
        List list = new ArrayList();
        List tmpList = new ArrayList();
        List todayList = apiDao.countByTime(start, end);
        Map map = new HashedMap();

        //统计少了哪个接口的数据是没有的
        for (Object ob : todayList) {
            Object[] ob1 = (Object[]) ob;
            tmpList.add(ob1[0]);
        }

        if (!tmpList.contains(SEG)) {
            Map map1 = new HashedMap();
            map1.put("name", SEG);
            map1.put("count", 0);

            list.add(map1);
        }
        if (!tmpList.contains(SEN)) {
            Map map1 = new HashedMap();
            map1.put("name", SEN);
            map1.put("count", 0);
            list.add(map1);
        }
        if (!tmpList.contains(DISCOURSE)) {
            Map map1 = new HashedMap();
            map1.put("name", DISCOURSE);
            map1.put("count", 0);
            list.add(map1);
        }
        if (!tmpList.contains(WEIBO)) {
            Map map1 = new HashedMap();
            map1.put("name", WEIBO);
            map1.put("count", 0);
            list.add(map1);
        }

        for (Object ob : todayList) {
            Object[] ob1 = (Object[]) ob;
            Map map1 = new HashedMap();
            map1.put("name", ob1[0]);
            map1.put("count", ob1[1]);
            list.add(map1);
        }
        map.put("count", list);
        resList.add(map);

        return resList;
    }

    /**
     * 获取所有的接口数据
     *
     * @return
     */
    @Override
    public List interfaceAll() {


        return findAll();
    }


}
